Imports System.Text.RegularExpressions
Imports System.IO



Public Class gVar

    ''''GLOBAL TYPES

    ''''Public Shared TITLE As String = "WinXound.Net 3.0.21 - "
    ''''Public Shared WINXOUND_VERSION As String = "3.0.21"
    ''The version number has four parts as follows: 
    ''<major version>.<minor version>.<build number>.<revision> 
    Public Shared WINXOUND_VERSION As String = My.Application.Info.Version.Major & "." & _
                                               My.Application.Info.Version.Minor & "." & _
                                               My.Application.Info.Version.Build
    Public Shared TITLE As String = "WinXound.Net " & WINXOUND_VERSION & " - "



    Public Structure mSyntax
        Public DefaultFontName As String
        Public DefaultFontSize As Int32
        Public DefaultTabSize As Int32

        Public TextForeColor As Int32
        Public TextBackColor As Int32
        Public TextSelectionForeColor As Int32
        Public TextSelectionBackColor As Int32

        Public OpcodesForeColor As Int32
        Public OpcodesBackColor As Int32
        Public OpcodesBold As Boolean
        Public OpcodesItalic As Boolean

        Public TagsForeColor As Int32
        Public TagsBackColor As Int32
        Public TagsBold As Boolean
        Public TagsItalic As Boolean

        Public MacrosForeColor As Int32
        Public MacrosBackColor As Int32
        Public MacrosBold As Boolean
        Public MacrosItalic As Boolean

        Public InstrForeColor As Int32
        Public InstrBackColor As Int32
        Public InstrBold As Boolean
        Public InstrItalic As Boolean

        Public StringsForeColor As Int32
        Public StringsBackColor As Int32
        Public StringsBold As Boolean
        Public StringsItalic As Boolean

        Public RemindersForeColor As Int32
        Public RemindersBackColor As Int32
        Public RemindersBold As Boolean
        Public RemindersItalic As Boolean

    End Structure

    Public Structure mDirectory
        Public CSoundConsole As String
        Public Winsound As String
        Public CSoundAV As String
        Public CSoundAVHelp As String
        Public CSoundHelp As String
        Public CSoundHelpHTML As String
        Public WaveEditor As String
        Public WorkingDir As String
        Public SFDIR As String
        Public SSDIR As String
        Public SADIR As String
        Public MFDIR As String
        Public INCDIR As String
    End Structure

    Public Structure mGeneral
        Public CSoundDefaultFlags As String
        Public MouseWheelScroll As Int32
        Public ShowUtilitiesMessage As Boolean
        Public ShowImportOrcScoMessage As Boolean
        Public WindowState As Windows.Forms.FormWindowState
        Public WindowSize As Point
        Public WindowPosition As Point
        Public FirstStart As Boolean
        Public FindWholeWord As Boolean
        Public FindMatchCase As Boolean
        Public ReplaceFromCaret As Boolean
        Public ShowTools As String
    End Structure

    Public Structure mPreferences
        Public General As mGeneral
        Public Syntax As mSyntax
        Public Directory As mDirectory
    End Structure

    Public Shared Preferences As mPreferences

    Public Shared LastFiles(4) As String

    Public Shared LastWordSearch As String = ""


    Public Enum mView
        FullCode = 1
        SplitHorizontal = 2
        SplitVertical = 3
        Compiler = 4
        Help = 5
    End Enum

    Public Enum CHECK_MODIFY
        mContinue = 1
        mStop = 0
    End Enum



    Public Shared Sub ToolbarEnable(ByVal mToolbar As ToolBar, ByVal mList() As Int32, ByVal mEnable As Boolean)
        For ciclo As Int32 = 0 To mList.Length - 1
            mToolbar.Buttons(mList(ciclo)).Enabled = mEnable
        Next
    End Sub

    Public Shared Sub InsertRecentFiles(ByVal mMenu As ToolStripMenuItem, ByVal mFileName As String)

        If File.Exists(mFileName) Then

            Dim mStrFound As Boolean = False
            Dim mInd As Byte = 0

            For Ciclo As Byte = 0 To 3
                If LastFiles(Ciclo) = mFileName Then
                    mStrFound = True
                    mInd = Ciclo
                End If
            Next

            If mStrFound = True Then
                For Ciclo As Int32 = mInd To 1 Step -1
                    LastFiles(Ciclo) = LastFiles(Ciclo - 1)
                Next
                LastFiles(0) = mFileName
            Else
                For Ciclo As Int32 = 3 To 1 Step -1
                    LastFiles(Ciclo) = LastFiles(Ciclo - 1)
                Next
                ''''LastFiles(3) = LastFiles(2)
                ''''LastFiles(2) = LastFiles(1)
                ''''LastFiles(1) = LastFiles(0)
                LastFiles(0) = mFileName
            End If

            For Ciclo As Byte = 0 To 3
                mMenu.DropDownItems(Ciclo).Text = "&" & (Ciclo + 1) & " " & LastFiles(Ciclo)
            Next

            'Dim mRegistry As New wxRegistry
            'mRegistry.SaveRecentFiles()
            Dim mSettings As New wxSettings
            mSettings.SaveRecentFiles()

        End If



    End Sub

    Public Shared Function RGBToColor(ByVal RGBColor As Int32) As Color

        'Convert from RGB value to Color structure
        Dim R, G, B As Int32

        R = RGBColor Mod 256
        G = (RGBColor Mod 65536) \ 256
        B = RGBColor \ 65536

        Return Color.FromArgb(R, G, B)

    End Function

    Public Shared Function ColorToRGB(ByVal mColor As Color) As Int32
        'Convert from Color structure to RGB value
        Return RGB(mColor.R, mColor.G, mColor.B)

    End Function


    Public Shared Sub SciEditSetFontsAndStyles(ByVal mSciEdit As SCI_EDIT)

        'FROM SCINTILLA (SCILEXER.H)
        '#define SCE_C_DEFAULT 0
        '#define SCE_C_COMMENT 1
        '#define SCE_C_COMMENTLINE 2
        '#define SCE_C_COMMENTDOC 3
        '#define SCE_C_NUMBER 4
        '#define SCE_C_WORD 5
        '#define SCE_C_STRING 6
        '#define SCE_C_CHARACTER 7
        '#define SCE_C_UUID 8
        '#define SCE_C_PREPROCESSOR 9
        '#define SCE_C_OPERATOR 10
        '#define SCE_C_IDENTIFIER 11
        '#define SCE_C_STRINGEOL 12
        '#define SCE_C_VERBATIM 13
        '#define SCE_C_REGEX 14
        '#define SCE_C_COMMENTLINEDOC 15
        '#define SCE_C_WORD2 16
        '#define SCE_C_COMMENTDOCKEYWORD 17
        '#define SCE_C_COMMENTDOCKEYWORDERROR 18
        '#define SCE_C_GLOBALCLASS 19


        For i As Int32 = 0 To 32
            mSciEdit.StyleSetFont(i, Preferences.Syntax.DefaultFontName)
            mSciEdit.StyleSetSize(i, Preferences.Syntax.DefaultFontSize)
        Next
        mSciEdit.SetZoom(0)
        mSciEdit.SetTabWidth(Preferences.Syntax.DefaultTabSize)


        'DEFAULT TEXT COLORS - (SCE_C_DEFAULT 0)
        mSciEdit.StyleSetFore(0, Preferences.Syntax.TextForeColor)
        mSciEdit.StyleSetBack(0, Preferences.Syntax.TextBackColor)

        '(SCE_C_IDENTIFIER 11)
        mSciEdit.StyleSetFore(11, Preferences.Syntax.TextForeColor)
        mSciEdit.StyleSetBack(11, Preferences.Syntax.TextBackColor)

        'Comment Multi /**/ - (SCE_C_COMMENT 1)
        mSciEdit.StyleSetFore(1, Preferences.Syntax.RemindersForeColor)
        mSciEdit.StyleSetBack(1, Preferences.Syntax.RemindersBackColor)
        mSciEdit.StyleSetBold(1, Preferences.Syntax.RemindersBold)
        mSciEdit.StyleSetItalic(1, Preferences.Syntax.RemindersItalic)

        'Comment Line ";" - (SCE_C_COMMENTLINE 2)
        mSciEdit.StyleSetFore(2, Preferences.Syntax.RemindersForeColor)
        mSciEdit.StyleSetBack(2, Preferences.Syntax.RemindersBackColor)
        mSciEdit.StyleSetBold(2, Preferences.Syntax.RemindersBold)
        mSciEdit.StyleSetItalic(2, Preferences.Syntax.RemindersItalic)

        'Numbers color - (SCE_C_NUMBER 4)
        mSciEdit.StyleSetFore(4, Preferences.Syntax.TextForeColor)
        mSciEdit.StyleSetBack(4, Preferences.Syntax.TextBackColor)

        'Keyword(0): OPCODES - (SCE_C_WORD 5)
        mSciEdit.StyleSetFore(5, Preferences.Syntax.OpcodesForeColor)
        mSciEdit.StyleSetBack(5, Preferences.Syntax.OpcodesBackColor)
        mSciEdit.StyleSetBold(5, Preferences.Syntax.OpcodesBold)
        mSciEdit.StyleSetItalic(5, Preferences.Syntax.OpcodesItalic)

        'Double quoted string - (SCE_C_STRING 6)
        mSciEdit.StyleSetFore(6, Preferences.Syntax.StringsForeColor)
        mSciEdit.StyleSetBack(6, Preferences.Syntax.StringsBackColor)
        mSciEdit.StyleSetBold(6, Preferences.Syntax.StringsBold)
        mSciEdit.StyleSetItalic(6, Preferences.Syntax.StringsItalic)

        'Preprocessor and Macro - (SCE_C_PREPROCESSOR 9)
        mSciEdit.StyleSetFore(9, Preferences.Syntax.MacrosForeColor)
        mSciEdit.StyleSetBack(9, Preferences.Syntax.MacrosBackColor)
        mSciEdit.StyleSetBold(9, Preferences.Syntax.MacrosBold)
        mSciEdit.StyleSetItalic(9, Preferences.Syntax.MacrosItalic)

        'Operator [=] - (SCE_C_OPERATOR 10)
        mSciEdit.StyleSetFore(10, Preferences.Syntax.OpcodesForeColor)
        mSciEdit.StyleSetBack(10, Preferences.Syntax.OpcodesBackColor)

        'CSD Tags, Keyword(1) - (SCE_C_WORD2 16)
        mSciEdit.StyleSetFore(16, Preferences.Syntax.TagsForeColor)
        mSciEdit.StyleSetBack(16, Preferences.Syntax.TagsBackColor)
        mSciEdit.StyleSetBold(16, Preferences.Syntax.TagsBold)
        mSciEdit.StyleSetItalic(16, Preferences.Syntax.TagsItalic)

        'INSTR/ENDIN color - Keyword(3) - (SCE_C_GLOBALCLASS 19)
        mSciEdit.StyleSetFore(19, Preferences.Syntax.InstrForeColor)
        mSciEdit.StyleSetBack(19, Preferences.Syntax.InstrBackColor)
        mSciEdit.StyleSetBold(19, Preferences.Syntax.InstrBold)
        mSciEdit.StyleSetItalic(19, Preferences.Syntax.InstrItalic)

        'DEFAULT STYLE "32"
        mSciEdit.StyleSetFore(32, Preferences.Syntax.TextForeColor)
        mSciEdit.StyleSetBack(32, Preferences.Syntax.TextBackColor)

        'CARET COLOR
        mSciEdit.SetCaretFore(Preferences.Syntax.TextForeColor)
        'SciEdit2.SetCaretFore(Preferences.Syntax.TextForeColor)

        'TEXT SELECTION
        mSciEdit.SetSelFore(True, Preferences.Syntax.TextSelectionForeColor)        'Fore Selection color
        mSciEdit.SetSelBack(True, Preferences.Syntax.TextSelectionBackColor)        'Back Selection color

    End Sub



    Public Shared Sub wxMessageError(ByVal mErrorMessage As String, ByVal mTitle As String)
        MessageBox.Show(mErrorMessage, mTitle, MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Sub


End Class







'*****************************************************************************************************
'*****************************************************************************************************
'*****************************************************************************************************
'*****************************************************************************************************
'*****************************************************************************************************
Public Class MyHashTable

    Inherits Hashtable
    Implements IComparable

    Public Function CompareTo(ByVal obj As Object) As Integer Implements System.IComparable.CompareTo
        Dim HT As Hashtable = DirectCast(obj, Hashtable)
        If HT.Count <> Me.Count Then
            Return -1
        Else
            For Each de As DictionaryEntry In Me
                If Not HT.Contains(de.Key) Then Return -1
            Next
        End If

        Return 0

    End Function

    Public Function ParseLine(ByVal Text As String) As String

        Dim dChars As String = "\s{1,}"
        ''Dim RegExp As New Regex("\s{1,}\d\s{1,}")
        Dim RegExp As New Regex("\d{1,3}")
        Dim ParsedText As String = RegExp.Replace(Text, dChars, " ")
        ''Dim ParsedText As String = Regex.Replace(Text, dChars, " ")

        ParsedText = ParsedText.Trim(" ")

        Dim MC As Match = RegExp.Match(ParsedText)
        If MC.Success Then
            If MC.Length = 1 Then
                Return ParsedText.Insert(MC.Index, "00")
            ElseIf MC.Length = 2 Then
                Return ParsedText.Insert(MC.Index, "0")
            Else
                Return ParsedText
            End If
        Else
            Return ParsedText
        End If

    End Function



End Class


